version 8.2
capture clear
capture log close
set more off
set mem 1000m
set mat 800


set scheme s2mono

****first germany:

use "Germany-2009-MOI-full data.dta"

/* moie17
If this establishment shut down its business, how long would it take your largest customers to find
an alternative supplier for its main product?
A day or less 1
More than a day, less than a week 2
More than a week, less than a month 3
A month or more 4
Never (it would be impossible to replace) 5
Don�t know (SPONTANEOUS) -9
*/

***To build the table for the paper: 
*sort moie17
*browse d1a1x d1a2 if moie17==5
*d1a1x: in the last complete fiscal year, what was this establishment�s main product?
*d1a2: please choose 4-digit code that best applies to main product of this est.

tab moie17
hist moie17 if moie17>0, percent
graph export "Figure 3a CI Germany.png", replace
*a day or less, more than a day, less than a week, more than a day, less than a week; more than a week, less than a month; a month or more; never (it would be impossible to replace)

gen rsi=1 if moie17==4|moie17==5
replace rsi=0 if moie17==1|moie17==2|moie17==3

tab rsi

keep d1a2 rsi

capture drop DJ4g*

local k=1

local list_variables rsi
foreach var of local list_variables{
	egen DJ4g`k'=mean(`var'), by(d1a2)
	local k=`k'+1
	}
	
sort d1a2 
keep if d1a2!=d1a2[_n-1]

***descriptive statistics
sum DJ4g1
centile(DJ4g1), centile(25 50 75)
display "25th percentile: `r(c_1)'"
display "50th percentile: `r(c_2)'"
display "75th percentile: `r(c_3)'"

gen DJ4g1m=1 if DJ4g1>=`r(c_2)'&DJ4g1!=.
replace DJ4g1m=0 if DJ4g1<`r(c_2)'

keep d1a2 DJ4g*
sort DJ4g1
*browse d1a2 DJ4g*
sort d1a2


hist DJ4g1, percent xtitle("Proportion per sector of firms answering 4 or 5")
graph export "Figure 3b CI Germany per sector.png", replace


save "JD_Germany_Innovation_4digit.dta", replace

****SWEDEN:

use "Enterprise surveys_clean.dta"

keep if countryname=="Sweden"
 
keep d1a2 herf input_output

capture drop DJ4s*

local k=1

local list_variables herf input_output
foreach var of local list_variables{
	egen DJ4s`k'=mean(`var'), by(d1a2)
	local k=`k'+1
	}
	
sort d1a2 
keep if d1a2!=d1a2[_n-1]

***descriptive statistics
sum DJ4s1
centile(DJ4s1), centile(25 75)
display "25th percentile: `r(c_1)'"
display "50th percentile: `r(c_2)'"
display "75th percentile: `r(c_3)'"

***descriptive statistics
forvalues i=1(1)2{
	sum DJ4s`i'
	centile(DJ4s`i'), centile(50)
	gen DJ4s`i'm=1 if DJ4s`i'>=`r(c_1)'&DJ4s`i'!=.
	replace DJ4s`i'm=0 if DJ4s`i'<`r(c_1)'
	}


keep d1a2 DJ4s*
sort d1a2
save "JD_Sweden_4digit.dta", replace


****By continent:

use "Enterprise surveys_clean.dta"


*star countries are: (rank in world, rank in zone) ease of doing business rank
*Europe: Sweden (9, 7), Estonia (12, 9)
*Asia: Malaysia (23, 4), Thailand (46, 5)
*Middle east: Morocco (68, 4), Tunisia (77, 5)
*Latin America: Mexico (47,1), Peru (54,2)
*Africa: Mauritius (49, 1), Rwanda (56, 2)


gen continent2="Europe" if countryname=="Sweden"|countryname=="Estonia"
replace continent2="Asia" if countryname=="Malaysia"|countryname=="Thailand"
replace continent2="Middle east" if countryname=="Morocco"|countryname=="Tunisia"
replace continent2="Latin America" if countryname=="Mexico"|countryname=="Peru"
replace continent2="Africa" if countryname=="Mauritius"|countryname=="Rwanda"

keep if continent2~=""

keep d1a2 continent herf input_output

local k=1
local list_variables herf input_output
foreach var of local list_variables{
	egen DJ4c`k'=mean(`var'), by(d1a2)
	local k=`k'+1
	}
	
sort continent d1a2 
keep if continent!=continent[_n-1]|(continent==continent[_n-1]&d1a2!=d1a2[_n-1])

***descriptive statistics
forvalues i=1(1)2{
	sum DJ4c`i'
	centile(DJ4c`i'), centile(50)
	gen DJ4c`i'm=1 if DJ4c`i'>=`r(c_1)'&DJ4c`i'!=.
	replace DJ4c`i'm=0 if DJ4c`i'<`r(c_1)'
	}

keep continent d1a2 DJ4c*
sort continent d1a2
save "JD_Continent_4digit.dta", replace




exit


